package com.real.fact.web.product.application;

import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.design.JasperDesign;

import com.globant.katari.core.application.Command;
import com.real.fact.web.application.GeneratePDFReportCommand;

public class GeneratePDFProductsReportCommand extends GeneratePDFReportCommand
    implements Command<Void> {

  private String searchFieldSelected;

  private String field;

  private boolean activeChecked;

  public GeneratePDFProductsReportCommand(DataSource theDataSource) {
    super(theDataSource);
  }

  @Override
  protected Map<String, Object> getParameters() {
    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("fieldName", searchFieldSelected);
    parameters.put("fieldValue", field);
    parameters.put("active", activeChecked);
    return parameters;
  }

  @Override
  protected String getReportName() {
    return "products";
  }

  public void setSearchFieldSelected(String searchFieldSelected) {
    this.searchFieldSelected = searchFieldSelected;
  }

  public void setField(String field) {
    this.field = field;
  }

  public void setActiveChecked(boolean activeChecked) {
    this.activeChecked = activeChecked;
  }

  @Override
  protected void customizeDesign(JasperDesign design) {
    String theQuery = "SELECT p.code AS code, p.name AS name, " +
    		"p.brand AS brand, p.category AS category, p.deskPrice AS desk_price," +
    		" p.stock AS stock " +
    		"FROM product p " +
    		"WHERE p." + searchFieldSelected + " LIKE CONCAT('%', '" + field + "','%')";
    		if (activeChecked == true)
    		  theQuery +=" AND active = " + activeChecked;
    JRDesignQuery newQuery = new JRDesignQuery();
    newQuery.setText(theQuery);
    design.setQuery(newQuery);
  }

  @Override
  public String getFileName() {
    return "productos_";
  }

}
